#!/bin/sh

# Variables from /etc/thingino.config:
# swap_enabled (true/false to determine if swap should be configured)
# swap_type (should be 'file' or 'partition')
# swap_file_path (if swap_type is 'file')
# swap_file_size (if swap_type is 'file', in M, ex. 256M)
# swap_partition_dev (if swap_type is 'partition')

. /usr/share/common

setup_swap_partition() {
	local partition="$1"

	if [ -z "$partition" ] || [ ! -b "$partition" ]; then
		echo_error "Invalid swap partition: $partition"
		return 1
	fi

	# Check if partition is already in fstab
	if ! grep -q "^$partition[[:space:]]\+none[[:space:]]\+swap" /etc/fstab; then
		echo "$partition none swap sw 0 0" >> /etc/fstab
		echo_info "Added swap entry for $partition to /etc/fstab"
	else
		echo_warning "Swap entry for $partition already exists in /etc/fstab"
	fi

	# Check if swap is already active
	if grep -q "$partition" /proc/swaps; then
		echo_warning "Swap partition $partition is already active"
		return 0
	fi

	# Try to enable swap directly - if it works, it's already formatted
	if swapon "$partition" 2>/dev/null; then
		echo_info "Enabled swap on $partition (already formatted)"
	else
		# Failed to swapon, format it and then enable
		echo_info "Formatting $partition as swap"
		mkswap "$partition"
		swapon "$partition"
		echo_info "Enabled swap on $partition (newly formatted)"
	fi

	return 0
}

setup_swap_file() {
	local swap_file="$1"
	local swap_dir=$(dirname "$swap_file")
	local size="${swap_file_size:-128M}"  # Default to 128M if not specified

	if [ -z "$swap_file" ]; then
		echo_error "Invalid swap file path: $swap_file"
		return 1
	fi

	# Check if directory exists
	if [ ! -d "$swap_dir" ]; then
		echo_error "Swap file directory does not exist: $swap_dir"
		return 1
	fi

	# Create swap file if it doesn't exist
	if [ ! -f "$swap_file" ]; then
		echo_info "Creating swap file: $swap_file (size: $size)"
		fallocate -l "$size" "$swap_file"
		chmod 600 "$swap_file"
		mkswap "$swap_file"

		# Add to fstab if not already there
		if ! grep -q "^$swap_file[[:space:]]\+none[[:space:]]\+swap" /etc/fstab; then
			echo "$swap_file none swap sw 0 0" >> /etc/fstab
			echo_info "Added swap entry for $swap_file to /etc/fstab"
		fi

		# Enable the newly created swap file
		swapon "$swap_file"
		echo_info "Enabled swap on $swap_file (newly created)"
	else
		# Check if already in fstab
		if ! grep -q "^$swap_file[[:space:]]\+none[[:space:]]\+swap" /etc/fstab; then
			echo "$swap_file none swap sw 0 0" >> /etc/fstab
			echo_info "Added swap entry for $swap_file to /etc/fstab"
		else
			echo_warning "Swap entry for $swap_file already exists in /etc/fstab"
		fi

		# Check if swap is already active
		if grep -q "$swap_file" /proc/swaps; then
			echo_warning "Swap file $swap_file is already active"
			return 0
		fi

		# Try to enable swap directly - if it works, it's already formatted
		if swapon "$swap_file" 2>/dev/null; then
			echo_info "Enabled swap on $swap_file (already formatted)"
		else
			# Failed to swapon, format it and then enable
			echo_info "Formatting $swap_file as swap"
			mkswap "$swap_file"
			swapon "$swap_file"
			echo_info "Enabled swap on $swap_file (newly formatted)"
		fi
	fi

	return 0
}

start() {
	if [ "$swap_enabled" != "true" ]; then
		echo_error "Swap is not enabled."
		exit 0
	fi

	if [ -z "$swap_type" ]; then
		echo_error "swap_type not defined."
		exit 1
	fi

	if [ ! -z "$swap_file_size" ]; then
		swap_file_size=$(echo "$swap_file_size" | tr '[:lower:]' '[:upper:]')
	fi

	echo_title "Configuring swap"
	case "$swap_type" in
		"partition")
			if [ -z "$swap_partition_dev" ]; then
				echo_error "swap_partition_dev not defined in config. Exiting."
				exit 1
			fi
			setup_swap_partition "$swap_partition_dev"
			;;
		"file")
			if [ -z "$swap_file_path" ]; then
				echo_error "swap_file_path not defined in config. Exiting."
				exit 1
			fi
			setup_swap_file "$swap_file_path"
			;;
		*)
			echo_error "Invalid swap_type: $swap_type (should be 'file' or 'partition')"
			exit 1
			;;
	esac
}

case "$1" in
	start)
		start
		;;
	stop)
		true
		;;
	restart)
		start
		;;
	*)
		echo "Usage: $0 {start|stop|restart}"
		exit 1
		;;
esac

exit $?